package com.top10.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.top10.bean.JwtUtil;
import com.top10.bean.PageBean;
import com.top10.bean.ResponseBean;
import com.top10.common.OrderConst;
import com.top10.entity.PayPoints;
import com.top10.entity.User;
import com.top10.entity.UserDeposit;
import com.top10.entity.UserWithdrawal;
import com.top10.service.IPayPointsService;
import com.top10.service.IUserDepositService;
import com.top10.service.IUserService;
import com.top10.service.IUserWithdrawalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author ellen
 * @version 1.0
 * @date 2018/10/18 20:03
 */
@RestController
@RequestMapping("/manager/withdrawal")
public class WithdrawalController {

	@Autowired
	IUserWithdrawalService userWithdrawalService;
	@Autowired
	IUserService userService;
	@Autowired
	IPayPointsService payPointsService;
	@Autowired
	IUserDepositService iUserDepositService;

	/**
	 * 用户提现申请(待完善)
	 *
	 * @param userWithdrawal
	 * @param request
	 * @return
	 */
	@RequestMapping("/add")
	public ResponseBean add(UserWithdrawal userWithdrawal, HttpServletRequest request) throws ParseException {
		if (userWithdrawal.getMoney() == null) {
			return ResponseBean.createError("提现金额不能为空");
		}

		String userUuid = JwtUtil.getUuidByToken(request);
		userWithdrawal.setUid(userUuid);
		userWithdrawal.setWithdrawType("1");
		userWithdrawal.setStatus(0);
		userWithdrawal.setCreatedAt(new Date());
		userWithdrawal.setUpdatedAt(new Date());

		User user = userService.selectOne(new EntityWrapper<User>().eq("uuid", userUuid));
		if (user.getMoney().compareTo(userWithdrawal.getMoney()) < 0) {
			return ResponseBean.createError("提现申请失败，余额不足");
		}

		PayPoints payPoints = payPointsService.selectOne(new EntityWrapper<PayPoints>().orderBy("tiime", false));
		if (payPoints.getLowerlimit() != null) {
			if (userWithdrawal.getMoney().doubleValue() < payPoints.getLowerlimit()) {
				return ResponseBean.createError("提现申请失败，用户单笔提现金额不能小于2元");
			}
		}

		if (payPoints.getToplimit() != null) {
			if (userWithdrawal.getMoney().doubleValue() > payPoints.getToplimit()) {
				return ResponseBean.createError("提现申请失败，用户单笔提现金额不能大于" + payPoints.getToplimit() + "元");
			}
		}

		//计算当天0点到24点
		Date date = new Date();
		int year = date.getYear() + 1900;
		int month = date.getMonth() + 1;
		int date2 = date.getDate();
		String startTime = year + "-" + month + "-" + date2 + " " + "00:00:00";
		String endTime = year + "-" + month + "-" + date2 + " " + "23:59:59";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date startDate = sdf.parse(startTime);
		Date endDate = sdf.parse(endTime);
		//查询当天提现记录
		List<UserWithdrawal> userWithdrawals = userWithdrawalService.selectList(new EntityWrapper<UserWithdrawal>()
				.eq("uid", userUuid)
				.between("created_at", startDate, endDate));

		//计算当天提现总金额
		double total = 0;
		for (UserWithdrawal uw : userWithdrawals) {
			double money = uw.getMoney().doubleValue();
			total += money;
		}

		if (payPoints.getBackcover() != null) {
			if (total >= payPoints.getBackcover()) {
				return ResponseBean.createError("提现申请失败，超出每日提现限度");
			}
		}

		userWithdrawalService.insert(userWithdrawal);

		return ResponseBean.createSuccess().setMessage("提现申请成功").addData("userWithdrawal", userWithdrawal);

	}


	/**
	 * 提现记录
	 * @param request
	 * @param pageBean
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/gettmoneyone")
	public ResponseBean getTiMoney(HttpServletRequest request,PageBean pageBean)throws Exception{
		/*String userUuid="b17200315c91433688e579f5acdcabe0";*/
		Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
		String userUuid = JwtUtil.getUuidByToken(request);
		List<UserWithdrawal> userWithdrawals = userWithdrawalService.selectList(new EntityWrapper<UserWithdrawal>()
				.eq("uid", userUuid)
				.eq("status",OrderConst.withdrawalstatus1).orderBy("created_at",false));
		 pageBean= new PageBean(page.getPageNum(),page.getPageSize(), (int) page.getTotal());
          return ResponseBean.createSuccess().addData("userWithdrawals",userWithdrawals).addData("pageBean",pageBean);
	}
	/**
	 *充值记录
	 * @param request
	 * @param pageBean
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getmoneytwo")
	public ResponseBean getChongMoney(HttpServletRequest request,PageBean pageBean)throws Exception{
		/*String userUuid="b16f09d28d4f48df9bacc9a3cc5013d9";*/
		Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
		String userUuid = JwtUtil.getUuidByToken(request);
		/*List<PayPoints> payPointsList= payPointsService.selectList(new EntityWrapper<PayPoints>().eq("uid",userUuid).eq("order_status",OrderConst.depositstatus1));*/
        List<UserDeposit> userDepositList=iUserDepositService.selectList(new EntityWrapper<UserDeposit>().eq("uid",userUuid).eq("pay_code",OrderConst.PAYCODE1).orderBy("order_time",false));
		pageBean= new PageBean(page.getPageNum(),page.getPageSize(), (int) page.getTotal());
		return ResponseBean.createSuccess().addData("userDepositList",userDepositList).addData("pageBean",pageBean);
	}
}